package graphic.dialogs;

import graphic.MainFrame;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utility.GraphicUtilsFactory;
import utility.Utility;
import core.Function;
import dbms.DBSelectingFunctions;

public class NewProgramToUserDialog extends AbstractDialog
{
	JComboBox userId;
	JComboBox programName;
		
	public NewProgramToUserDialog()
	{	
		super("Add New Program", "Add Program", 450, 250);
	}
	
	public void createPanel()
	{
		super.setPanel(new JPanel(new GridBagLayout()));
		
		JPanel panel = super.getPanel(); 
		
		try {
			this.userId = new JComboBox(Utility.resultSetToVector(DBSelectingFunctions.getUsers()));
			this.programName = new JComboBox(Utility.resultSetToVector(DBSelectingFunctions.getNotAssociatedPrograms((String)this.userId.getSelectedItem())));
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.userId, "User Name");
		GraphicUtilsFactory.setTitleAndBorderToComponent(this.programName, "Program Name");
		
		this.userId.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{	
				try {
					Vector<Object> programs = Utility.resultSetToVector(DBSelectingFunctions.getNotAssociatedPrograms((String)NewProgramToUserDialog.this.userId.getSelectedItem()));
					if(programs.isEmpty())
					{
						NewProgramToUserDialog.this.programName.removeAllItems();
						return;
					}
					
					NewProgramToUserDialog.this.programName.removeAllItems();
					for (Iterator iterator = programs.iterator(); iterator.hasNext();) {
						String program = (String) iterator.next();
						NewProgramToUserDialog.this.programName.addItem(program);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(5, 10, 5, 10);
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Adding Program to User"), c);
		
		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.userId, c);		

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		panel.add(this.programName, c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);

		c.gridx = 1;
		c.gridy = 3;
		panel.add(super.getExitButton(), c);
	}

	@Override
	public boolean checkOkFunction()
	{
		if(this.userId.getSelectedItem().equals(""))
		{
			JOptionPane.showMessageDialog(this, "Please select an User Name!", "Error during the procedure!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		if(this.userId.getSelectedItem().equals(""))
		{
			JOptionPane.showMessageDialog(this, "Please select a program name!", "Error during the procedure!", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		return true;
	}

	@Override
	public void okFunction()
	{
		if(Function.addProgramToUser((String)this.programName.getSelectedItem(), (String)this.userId.getSelectedItem()))
		{
			super.dispose();
			MainFrame.getInstance().showMessage("Program added to the User!", JOptionPane.INFORMATION_MESSAGE);
		} else {
			super.dispose();
			MainFrame.getInstance().showMessage("Program NOT added to the User!", JOptionPane.ERROR_MESSAGE);
		}		
	}
}